package com.xuzimian.globaldemo.aws.cloud.s3.common;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicSessionCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.securitytoken.AWSSecurityTokenService;
import com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClientBuilder;
import com.amazonaws.services.securitytoken.model.Credentials;
import com.amazonaws.services.securitytoken.model.GetSessionTokenRequest;
import com.amazonaws.services.securitytoken.model.GetSessionTokenResult;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class AWSTokenServiceHelper {

    /**
     * 创建临时凭证
     *
     * @param sts_client
     * @return
     */
    public static Credentials getCredentials(AWSSecurityTokenService sts_client) {
        //创建 GetSessionTokenRequest 对象，还可以设置临时凭证的有效期 (秒)：
        GetSessionTokenRequest session_token_request = new GetSessionTokenRequest();
        session_token_request.setDurationSeconds(60 * 60 * 2);

        //在 STS 客户端上调用 getSessionToken 以获取会话令牌 (使用 GetSessionTokenRequest 对象)：
        GetSessionTokenResult session_token_result =
                sts_client.getSessionToken(session_token_request);

        //使用调用 getSessionToken 的结果获取会话凭证：
        return session_token_result.getCredentials();
    }

    public static Credentials getStsCredentials() {
        AWSSecurityTokenService sts_client = getStsService("https://sts.us-east-2.amazonaws.com/",
                "us-east-2");

        Credentials session_creds = createChangelessCredentials();
        System.out.println(session_creds);
        return session_creds;
    }

    /**
     * 使用临时服务凭证创建 S3 客户端：
     *
     * @param session_creds
     * @return
     */
    public static AmazonS3 getAmazonS3(Credentials session_creds) {
        BasicSessionCredentials sessionCredentials = new BasicSessionCredentials(
                session_creds.getAccessKeyId(),
                session_creds.getSecretAccessKey(),
                session_creds.getSessionToken());

        return AmazonS3ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(sessionCredentials))
                .build();
    }

    /**
     * 创建STS Service
     *
     * @param serviceEndpoint
     * @param signingRegion
     * @return
     */
    public static AWSSecurityTokenService getStsService(String serviceEndpoint, String signingRegion) {
        return AWSSecurityTokenServiceClientBuilder.standard()
                .withEndpointConfiguration(new AwsClientBuilder
                        .EndpointConfiguration(serviceEndpoint, signingRegion))
                .build();
    }

    public static Credentials createChangelessCredentials() {
        //Credentials credentials = getCredentials(sts_client);AWSSecurityTokenService sts_client
        return new Credentials("ASIAXWHS6XLLD4WRVCFC",
                "S62MdRwQmBFAxf11tvVk+06eRCpamHLLLrS2WrJj",
                "IQoJb3JpZ2luX2VjEEQaCXVzLWVhc3QtMiJGMEQCIALcKyk/tD9HqvjJYh33m08oFWaTtrQkYAbqGr6n/QbIAiAxDoWLiboANUTyX1cfluqvgeBnsxgbRDr0ap+gUOWN1CqtAQjt//////////8BEAAaDDUyODc5MDUwMjEwMiIMAmyfnH/T2jRhXlcxKoEB2Q699enb/Ujh+Zek0M7rCfXhdgcyMmvCM9gbWjL5R/Ufb+eqflNZVLMNY+Wo5BbD5DUZodHWxHpkUKrX2h/QE/jS7RX4nRIQkO8ZdTlJTIoQmGar6ion+XlWn9ZapODi9iVrnCiqPOy2wQtVwA7k+WyZHcstZjJhqGD6kM1Q1TnKMPzi//EFOt8ByXVjXn0owCUumyMPqu2Ldhw8SR/65dxuacm59PNJauHWTkkpvGF+gGOPmbDkGlUDV5iM9Cx6K53iHeWPU8HuuMPvZpv+1LkVR8GPRzElvAOxRTQMQi0u7SWkKWKXNvnHB8993bCcoqcUfvD4AKKvrOQxyITjhzEda+6kWmybsbzuFEZOLPcD2uhj03AIywKsTr9v8OzLwOoKzz1OqaybuucspL39zmPgDEIN5KO/yNAzfvmCQLkvkSpmEjEOpWzBrBxkabCWzMwFPRkE9ZwUycJi7+vsEy4wwpuUBIUvEg==",
                getExpiration());
    }

    private static Date getExpiration() {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        Date date1 = null;
        try {
            date1 = sf.parse("2020-02-08 21:15");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date1;
    }
}
